package com.ew.gerocomium.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ew.gerocomium.common.constant.YesNoEnum;
import com.ew.gerocomium.common.util.PageUtil;
import com.ew.gerocomium.dao.base.PageResult;
import com.ew.gerocomium.dao.base.Result;
import com.ew.gerocomium.dao.mapper.NoticeMapper;
import com.ew.gerocomium.dao.po.Notice;
import com.ew.gerocomium.dao.query.PageNoticeByKeyQuery;
import com.ew.gerocomium.dao.vo.PageNoticeByKeyVo;
import com.ew.gerocomium.service.NoticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private PageUtil pageUtil;
    @Override
    public Result pageNoticeByKey(PageNoticeByKeyQuery query) {
//         获取所有未被删除的公告
        List<Notice> NoticeList = noticeMapper.selectByQueryOrderByTime(query.getTitle());
        // 列表实体转换
        List<PageNoticeByKeyVo> pageNoticeByKeyVoList = BeanUtil.copyToList(NoticeList, PageNoticeByKeyVo.class);
        // 封装返回数据
        PageResult<PageNoticeByKeyVo> pageResult = pageUtil.packPageResultData(pageNoticeByKeyVoList, query.getPageNum(), query.getPageSize());
        return Result.success(pageResult);

    }
    @Override
    public Result addNotice(String title, String content) {

        // 初始化来源渠道
        Notice notice = new Notice();
        notice.setTitle(title);
        notice.setContent(content);
        notice.setDelFlag(YesNoEnum.NO.getCode());
        // 新增
        noticeMapper.insert(notice);
        return Result.success();
    }

    @Override
    public Result delNotice(Long id) {
        // 验证事故登记
        Notice  notice = noticeMapper.selectById(id);
        // 封装修改
        notice.setDelFlag(YesNoEnum.YES.getCode());
        // 修改
        noticeMapper.updateById(notice);
        return Result.success();

    }

}
